package org.util.xmlrpc;

import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.util.common.ReflectUtil;


public class RpcClient {

	private String adminUser = "admin";
	private String adminPw = "admin";

	private String loginUser;
	private String loginPw;

	private XmlRpcClient client;
	private Vector<Object> params;

	private String xmlRpcUrl;

	private String customizeMethod = "extapi.callWpMethod";

	public void init() throws Exception {
		xmlRpcUrl = "http://192.168.163.44/wordpress/xmlrpc.php";
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL(xmlRpcUrl));
		config.setBasicPassword("admin");
		config.setBasicUserName("admin");
		config.setEnabledForExtensions(true);
		config.setEnabledForExceptions(true);
		client = new XmlRpcClient();
		client.setConfig(config);
		params = new Vector<Object>();
	}

	public String wp_create_user(String username, String password, String email)
			throws Exception {
		init();
		LinkedHashMap<String, String> post = new LinkedHashMap<String, String>();
		post.put("username", username);
		post.put("password", password);
		post.put("email", email);
		params.addElement(adminUser);
		params.addElement(adminPw);
		params.addElement("wp_create_user");
		params.addElement(post);
		return client.execute(customizeMethod, params).toString();
	}

	public String wp_create_category(String category) throws Exception {
		init();
		LinkedHashMap<String, String> post = new LinkedHashMap<String, String>();
		post.put("cat_name", category);
		params.addElement(adminUser);
		params.addElement(adminPw);
		params.addElement("wp_create_category");
		params.addElement(post);
		Object object = client.execute(customizeMethod, params);
		System.out.println(object.getClass().getSimpleName());
		return  object.toString();

	}
	
	
	public String wp_create_new_post(String userName,String password,String categoryId,Parameters postInfo) throws Exception{
		init();
		params.addElement(0);
		
		params.addElement(userName);
		params.addElement(password);
		
		LinkedHashMap<String, Object> postHashMap = new LinkedHashMap<String, Object>();
		
		Map<String,Object> parameters = ReflectUtil.getFileValueMap(postInfo);
		if(parameters!=null && parameters.size()>0){
			Set<String> keys = parameters.keySet();
			for(String key : keys){
				postHashMap.put(key, parameters.get(key));
			}
		}
		LinkedHashMap<String, Object> terms = new LinkedHashMap<String, Object>();
		List<String> termIds = new ArrayList<String>();
		termIds.add(categoryId);
		terms.put("category", termIds);
		postHashMap.put("terms", terms);
		params.add(postHashMap);
		return client.execute("wp.newPost", params).toString();
	}


	public Parameters[] wp_get_posts_by_category(String categoryId, int number,int offset)
			throws Exception {
		/**
		 * int blog_id 
		 * string username 
		 * string password 
		 * struct filter: Optional.
		 * string post_type string post_status int number int offset string
		 * orderby string order
		 */
		init();
		params.addElement(0);
		params.addElement(adminUser);
		params.addElement(adminPw);
		LinkedHashMap<String, Object> post = new LinkedHashMap<String, Object>();
		post.put("post_type", "post");
		post.put("post_category", categoryId);
		post.put("number", number);
		post.put("offset", offset);
		params.addElement(post);
		Object[] results = (Object[]) client.execute("wp.getPosts", params);
		Parameters[] postParameters = new Parameters[results.length];
		for(int i = 0;i<results.length;i++){
			Map<String,Object> result = (Map<String, Object>) results[i];
			Set<String> keys = result.keySet();
			Parameters parameter = new Parameters();
			for(String key : keys){
				Object value = result.get(key);
				ReflectUtil.setFieldValue(parameter, key, value);
			}
			postParameters[i]= parameter;
		}
		return postParameters;
	}
	
	public Boolean wp_edit_post(String username,String password,String postId,Parameters postInfo)throws Exception{
		init();
		params.addElement(0);
		params.addElement(username);
		params.addElement(password);
		params.addElement(postId);
		
		LinkedHashMap<String, Object> postHashMap = new LinkedHashMap<String, Object>();
		
		Map<String,Object> parameters = ReflectUtil.getFileValueMap(postInfo);
		if(parameters!=null && parameters.size()>0){
			Set<String> keys = parameters.keySet();
			for(String key : keys){
				postHashMap.put(key, parameters.get(key));
			}
		}
		params.add(postHashMap);
		return (Boolean) client.execute("wp.editPost", params);
	}
}
